const gwc = require('../../models/gwc')
const goods = require('../../models/goods');
const { login } = require('../login');


exports.getUsergwc = async function (userId) {
    console.log(userId);
    const goodsid = await gwc.findAll({ //查询配置，查询条件
        where: {
            userId
        },
        attributes: ['goodsId', 'num'],
        raw: true
    }).catch(() => { })
    // console.log(goodsid);
    let a = goodsid.map(item => item.goodsId);
    console.log(a);
    const result = await goods.findAll({ //查询配置，查询条件
        where: {
            id: a
        },
        raw: true
    }).catch(() => { })
    console.log(result);
    console.log(goodsid);

    // let obj = {}
    for (var i = 0; i < result.length; i++) {
        for (var j = 0; j < goodsid.length; j++) {
            if (result[i].id == goodsid[j].goodsId) {
                result[i].num = goodsid[j].num
            }
        }
    }
    console.log(result.map(item => item.couponId));
    if (result != '') return {
        data: result,
        meta: {
            msg: '查询成功',
            status: '200'
        }
    }
    return {
        data: {},
        meta: {
            msg: '暂无数据或用户不存在',
            status: '404'
        }
    }
}


exports.addUsergwc = async function (userId, goodsId, num) {
    const result = await gwc.findOne({ //查询配置，查询条件
        where: {
            userId,
            goodsId,
            num
        },
        raw: true
    }).catch(() => { })
    console.log(result);
    if(result) return
    gwc.create({
        userId,
        goodsId,
        num
    }).catch(() => { })
    return true
}

exports.updateUsernum = async function (userId, goodsId, num) {
    gwc.update(
        {
            num: num
        }, {

        where: {
            userId,
            goodsId
        }
    }
    )
    return true
}